package main

import (
	"fmt"
	"html/template"
	"net/http"
)

func form(w http.ResponseWriter,r *http.Request)  {
	switch r.Method {
	case "GET":
		t, err := template.ParseFiles("./get.tmpl")
		if err != nil {
			fmt.Printf("parse file failed err := %v",err)
		}
		err = t.Execute(w, nil)
		if err != nil {
			fmt.Printf("execute file failed err := %v",err)
		}
	case "POST":
		t, err := template.ParseFiles("./post.tmpl")
		if err != nil {
			fmt.Printf("parse file failed err := %v",err)
		}
		name := r.FormValue("name")
		//fmt.Printf("name = %s\n",name)
		sex := r.FormValue("sex")
		//fmt.Printf("sex = %s\n",sex)
		age := r.FormValue("age")
		//fmt.Printf("age = %s\n",age)
		m := map[string]interface{}{
			"name":name,
			"sex":sex,
			"age":age,
		}
		err = t.Execute(w, m)
		if err != nil {
			fmt.Printf("execute file failed err := %v",err)
		}
	}


}

func main() {
	// 配置路由
	http.HandleFunc("/",form)

	// 启动
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Printf("listen address failed err = %v",err)
	}
}
